﻿using StoreProject.BLL;
using StoreProject.BLL.SettingBLL;
using StoreProject.Entity;
using StoreProject.Entity.SettingEntity;
using StoreProject.Enum;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StoreProject.Forms.WorkingProcedure
{
    public partial class WorkingProcedureSettingForm : Form
    {
        WorkingProcedureEntity entity = null;
        WorkingProcedureBLL _bll = new WorkingProcedureBLL();
        WorkingProcedureNameBLL workingProcedureNameBLL = new WorkingProcedureNameBLL();
        WorkingProcedureRemarkBLL workingProcedureRemarkBLL = new WorkingProcedureRemarkBLL();
        protected FormState _formState = FormState.INIT;
        public WorkingProcedureSettingForm(FormState state)
        {
            _formState = state;
            InitializeComponent();
        }
        public WorkingProcedureSettingForm(FormState state,WorkingProcedureEntity entity)
        {
            _formState = state;
            this.entity = entity;
            InitializeComponent();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {

            WorkingProcedureEntity model = GetModel();
            bool res = false;
            if (_formState == FormState.ADD)
            {
                res = _bll.Insert(model);
            }
            else if (_formState == FormState.UPDATE)
            {
                res = _bll.Update(model);
            }
            if (res)
            {
                MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("保存失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private WorkingProcedureEntity GetModel()
        {
            if (entity == null)
            {
                entity = new WorkingProcedureEntity();
                entity.Id = Guid.NewGuid().ToString();
                entity.CreateTime = DateTime.Now;
                entity.UpdateTime = DateTime.Now;
            }
            else
            {
                entity.UpdateTime = DateTime.Now;
            }
            entity.Remark = this.cmbRemark.Text;
            entity.IndexNum = Convert.ToInt32(this.numIndex.Text);
            entity.IndexName = this.cmbName.Text;
            return entity;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void WorkingProcedureSettingForm_Load_1(object sender, EventArgs e)
        {
            cmbName.DisplayMember = "WorkingProcedureName";
            cmbName.ValueMember = "WorkingProcedureName";
            cmbRemark.DisplayMember = "WorkingProcedureRemark";
            cmbRemark.ValueMember = "WorkingProcedureRemark";
            InitData();
        }
        private void InitData()
        {
            var workingProcedureName = workingProcedureNameBLL.GetList().ToList();
            cmbName.DataSource = workingProcedureName;

            var workingProcedureRemark = workingProcedureRemarkBLL.GetList().ToList();
            cmbRemark.DataSource = workingProcedureRemark;
            if (_formState == FormState.ADD)
            {
                this.Text = "新增工序信息";
            }
            else if (_formState == FormState.UPDATE)
            {
                this.Text = "修改工序信息";
            }
            if (this.entity != null)
            {
                this.cmbRemark.Text = entity.Remark;
                this.numIndex.Text = entity.IndexNum.ToString();
                this.cmbName.Text = entity.IndexName;
            }
        }

        private void numIndex_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
                e.Handled = true;
        }
    }
}
